<?php
/**
 * Created by PhpStorm.
 * User: majiawen
 * Date: 2018/2/26
 * Time: 14:59
 */

namespace app\admin\controller\v1;

use app\admin\controller\Fun;
use app\common\Model\UserShaohuoType;
use think\Db;

/**
 * @title 后台-捎货管理-捎货人管理-捎货人身份类型-V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class ShaohuoUserType extends Base
{
    /**
     * @title 捎货人身份类型列表
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUserType desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:ShaohuoUserTypeList desc:
     *
     * @param name:page type:string require:1 default: other: desc:页码
     * @param name:page_size type:string require:1 default: other: desc:每页个数
     * @param name:search type:string require:1 default: other: desc:查询数据
     * @param name:sort_field type:string require:1 default: other: desc:排序字段
     * @param name:sort_type type:string require:1 default: other: desc:0正序1倒序
     *
     * @return data:[]
     */
    public function ShaohuoUserTypeList()
    {
        $UserShaohuoType = new UserShaohuoType();
        $where['is_del'] = $UserShaohuoType::IS_DEL_NO;
        //查询
        if (isset($this->param['search']) && $this->param['search'] != '') {
            $search = $this->param['search'];
            $where['name'] = ['like', '%' . $search . '%'];
        }
        if (isset($this->param['sort_field']) && $this->param['sort_field'] != '' && in_array($this->param['sort_field'], $UserShaohuoType->sort_field) && isset($this->param['sort_type'])) {
            $order = [
                $this->param['sort_field'] => $this->sort_type[intval($this->param['sort_type'])]
            ];
        } else {
            $order = ['create_time' => $this->sort_type[0]];
        }
        $list = Db::name('user_shaohuo_type')
            ->where($where)
            ->order($order)
//            ->field('fc_id id,func_name name')
            ->paginate($this->page_size,false,['page'=>$this->param['page']]);
        if ($list) {
            $data = $list->items();
            $response = [];
            foreach ($data as $key => $value) {
                $fc_ids = explode(',', $value['fc_ids']);
                $options = Db::name('func_class')->where(['fc_id' => ['in', $fc_ids]])->column('fc_id,func_name');
                $response[] = [
                    'ust_id' => $value['ust_id'],
                    'name' => $value['name'],
                    'status' => $UserShaohuoType->_status[$value['status']],
                    '_status' => $value['status'],
                    'func_names' => $options,
                    'create_time' => $value['create_time'],
                    'update_time' => $value['update_time'],
                ];
            }
            return Fun::success(['list' => $response, 'total_num' => $list->total()]);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }
    /**
     * @title 全部捎货人身份类型(用于下拉框)
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUserType desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:ShaohuoUserType desc:全部捎货人身份类型(用于下拉框)
     *
     * @return data:[]
     */
    public function ShaohuoUserType()
    {
        $UserShaohuoType = new UserShaohuoType();
        $list = $UserShaohuoType->where('is_del',$UserShaohuoType::IS_DEL_NO)->where('status',$UserShaohuoType::STATUS_NORMAL)->field('ust_id,name,fc_ids')->select();
        if ($list) {
            return Fun::success(['list' => $list]);
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 添加捎货人身份类型
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUserType desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:addShaohuoType desc:
     *
     * @param name:fc_ids type:string require:1 default: other:1,2,3 desc:捎货类型ID
     * @param name:name type:string require:1 default: other:普通捎货人 desc:捎货种类命名
     *
     * @return data:[]
     */
    public function addShaohuoType()
    {
        if (empty($this->param['fc_ids']) || empty($this->param['name'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }

        $fc_ids = explode(',', trim($this->param['fc_ids'], ','));
        $fc_ids = \app\common\Model\FuncClass::where('fc_id', 'in', $fc_ids)->column('fc_id');
        $fc_ids = implode(',', $fc_ids);
        if (empty($fc_ids)) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }

        $data = [
            'name' => $this->param['name'],
            'fc_ids' => $fc_ids,
            'status' => 1,
            'is_del' => 0,
        ];
        $res = Db::name('user_shaohuo_type')
            ->insert($data);
        if ($res) {
            return Fun::success('');
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 修改捎货人身份类型
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUserType desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:alterShaohuoType desc:
     *
     * @param name:ust_id type:string require:1 default: other:1 desc:捎货人身份类型ID
     * @param name:fc_ids type:string require:1 default: other:1,2,3 desc:捎货类型ID
     * @param name:name type:string require:1 default: other:普通捎货人 desc:捎货种类命名
     *
     * @return data:[]
     */
    public function alterShaohuoType()
    {
        if (empty($this->param['fc_ids']) || empty($this->param['name']) || empty($this->param['ust_id'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }

        $fc_ids = explode(',', trim($this->param['fc_ids'], ','));
        $fc_ids = \app\common\Model\FuncClass::where('fc_id', 'in', $fc_ids)->column('fc_id');
        $fc_ids = implode(',', $fc_ids);
        if (empty($fc_ids)) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }

        $data = [
            'ust_id' => $this->param['ust_id'],
            'name' => $this->param['name'],
            'fc_ids' => $fc_ids,
            'status' => 1,
            'is_del' => 0,
        ];
        $res = Db::name('user_shaohuo_type')
            ->update($data);
        if ($res) {
            return Fun::success('');
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }

    /**
     * @title 修改捎货人身份类型的状态
     * @description
     * @author majiawen
     * @url /admin/index/index
     * @method POST
     *
     * @header name:interface require:1 default:ShaohuoUserType desc:接口组(全局)
     * @header name:access_token type:string require:1 default: other: desc:身份验证参数,登陆时返回
     *
     * @param name:acc_id type:string require:1 default: other: desc:身份验证参数,登陆时返回
     * @param name:command type:string require:1 default: other:alterShaohuoTypeStatus desc:
     *
     * @param name:ust_ids type:string require:1 default: other:1,2,3 desc:捎货人身份类型ID
     * @param name:status type:string require:1 default: other:1 desc:捎货类型状态1有效,0禁用,2删除
     *
     * @return data:[]
     */
    public function alterShaohuoTypeStatus()
    {
        if (empty($this->param['ust_ids'])) {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        $ust_ids = explode(',', trim($this->param['ust_ids'], ','));
        $status = intval($this->param['status']);
        //状态:1有效,0禁用,2时is_del为1
        if ($status == 0 || $status == 1) {
            $data['status'] = $status;
        } elseif ($status == 2) {
            $data['is_del'] = 1;
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
        $res = Db::name('user_shaohuo_type')
            ->where([
                'ust_id' => ['in', $ust_ids]
            ])
            ->update($data);
        if ($res) {
            return Fun::success('');
        } else {
            return Fun::error('');//TODO 无数据，定义错误类型
        }
    }
}